package hy.controller;

import com.alibaba.excel.EasyExcel;
import hy.beans.ExcelModel;
import hy.config.ExcelListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
public class FileProcess {
    @RequestMapping("upExcel")
    public void uploadExcel(MultipartFile file) throws IOException {
        ExcelListener listener = new ExcelListener();  //ExcelListener  上面创建的那个类
        EasyExcel.read(file.getInputStream(), ExcelModel.class,listener).sheet("Sheet1").doReadSync();  //ExcelModel 上面创建的实体类
        System.out.println("解析完之后的数据：---"+listener.getDatas());
    }
    @RequestMapping("/downExcel")
    public void downloadExcel(HttpServletResponse resp) throws Exception {
        resp.setContentType("application/vnd.ms-excel");
        List<ExcelModel> list = Arrays.asList(new ExcelModel("a1",23,"lly@123.com"),new ExcelModel("a2",15,"abc@123.com"));
        resp.setHeader("Content-disposition", "attachment;filename=hello.xlsx");
        EasyExcel.write(resp.getOutputStream(),ExcelModel.class).sheet("sheet1").doWrite(list);
    }
}